#include <stdio.h>
#include <stdlib.h>

typedef struct ListNode {
    int data;
    struct ListNode* next;
} LN;

// 创建链表
LN* createList() {
    LN *head = NULL, *tail = NULL;
    int number;

    while (1) {
        scanf("%d", &number);
        if (number == -1) break;

        LN* newnode = (LN*)malloc(sizeof(LN));
        newnode->data = number;
        newnode->next = NULL;

        if (tail == NULL) {
            head = newnode;
            tail = newnode;
        } else {
            tail->next = newnode;
            tail = newnode;
        }
    }
    return head;
}

// 构造交集链表
LN* intersection(LN* list1, LN* list2) {
    LN* result = NULL;
    LN* tail = NULL;

    while (list1 && list2) {
        if (list1->data < list2->data) {
            list1 = list1->next;
        } else if (list1->data > list2->data) {
            list2 = list2->next;
        } else {
            LN* newnode = (LN*)malloc(sizeof(LN));
            newnode->data = list1->data;
            newnode->next = NULL;

            if (tail == NULL) {
                result = newnode;
                tail = newnode;
            } else {
                tail->next = newnode;
                tail = newnode;
            }

            list1 = list1->next;
            list2 = list2->next;
        }
    }
    return result;
}

// 释放链表内存
void freeList(LN* head) {
    LN* temp;
    while (head != NULL) {
        temp = head;
        head = head->next;
        free(temp);
    }
}

// 打印链表
void printList(LN* head) {
    if (head == NULL) {
        printf("NULL\n");
        return;
    }

    LN* current = head;
    while (current) {
        printf("%d", current->data);
        if (current->next) {
            printf(" ");
        }
        current = current->next;
    }
    printf("\n");
}

int main() {
    // 创建两个链表
    LN* list1 = createList();
    LN* list2 = createList();

    // 获取交集链表
    LN* list3 = intersection(list1, list2);

    // 打印结果
    printList(list3);

    // 释放链表内存
    freeList(list1);
    freeList(list2);
    freeList(list3);

    return 0;
}
